#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int main() {
	int go[] = {
		8 * 60, 9 * 60 + 43 , 11 * 60 + 19, 12 * 60 + 47,
		14 * 60, 15 * 60 + 45, 19 * 60, 21 * 60 + 45,
	};

	int arrive[] = {
		10 * 60 + 16, 11 * 60 + 52, 13 * 60 + 31, 15 * 60,
		16 * 60 + 8, 17 * 60 + 55, 21 * 60 + 20, 11 * 60 + 58,
	};

	int diff[8] = { 0 };

	int hour, minute, total_minutes, min_minute = INT_MAX, index;

	printf("Enter a 24-hour time: ");
	scanf("%d:%d", &hour, &minute);

	total_minutes = hour * 60 + minute;

	for (int i = 0; i < 8; i++)
	{
		diff[i] = abs(total_minutes - go[i]);
	}

	for (int i = 0; i < 8; i++)
	{
		if (min_minute > diff[i])
		{
			min_minute = diff[i];
			index = i;
		}
	}

	printf("Closest departure time is %d:%d %c.m., arriving at %d:%d %c.m.\n", 
		go[index] / 60,
		go[index] % 60,
		go[index] > 12 * 60 ? 'p' : 'a',

		arrive[index] / 60,
		arrive[index] % 60,
		arrive[index] > 12 * 60 ? 'p' : 'a'
		);

	return 0;
}